<?php

include "../config.php";
$db		=	new	lg_mysql($host,$dbuser,$dbpass,$csdl);
include "../func.php";

function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
    
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
    
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

$xmlUrl = "http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx";
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
$time 	= $arrXml["DateTime"];
$source = $arrXml['Source'];
foreach ($arrXml['Exrate'] as $xml)
{
	$exrate = $xml['@attributes'];
	$currency = Currency::getCurrencyByCode($exrate['CurrencyCode'])
	
	if ($currency)
	{
		$currency->setBuy($exrate['Buy']);
		$currency->setTransfer($exrate['Transfer']);
		$currency->setSell($exrate['Sell']);
		$currency->settime($time);
		$currency->setsource($source);
		$currency->update();
		echo "OK-";
	}
	else
	{
		$currency->setBuy($exrate['Buy']);
		$currency->setTransfer($exrate['Transfer']);
		$currency->setSell($exrate['Sell']);
		$currency->settime($time);
		$currency->setsource($source);
		$currency->insert();
		echo "OK-";
	}
}

?>